import argparse
from model.net import CLSNet_Attention, CLSNetV3Attr
from data.tvm_dataset import TVMDataSetV2
import torch
import datetime
import os
from torch.utils.data import DataLoader
import tqdm
from loguru import logger
import sys
from my_utils.log_util import LogUtil
# import pickle

from my_utils.parsing_util import SLOT_LENGTH, SLOT_COUNT

from my_utils.eval_util import eval_attr_v2

label_list = [
    'divide', 'abs', 'expand_dims', 'negative', 'upsampling', 'batch_matmul', 'global_max_pool2d', 'subtract',
    'minimum', 'concatenate', 'add', 'relu', 'bias_add', 'global_avg_pool2d', 'exp', 'cast', 'multiply', 'clip',
    'upsampling3d', 'tanh', 'max_pool3d', 'log', 'strided_slice', 'pad', 'avg_pool3d', 'max_pool2d', 'maximum', 'dense',
    'transpose', 'sqrt', 'sigmoid', 'adaptive_avg_pool3d', 'batch_flatten', 'conv3d', 'avg_pool2d', 'softmax',
    'greater', 'leaky_relu', 'adaptive_max_pool3d', 'conv2d'
]


def run(args):
    # Create the results dir
    if not os.path.exists("./results"):
        os.mkdir("./results")

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    current_time = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")

    logfile_path = LogUtil.create_dir(args, current_time, os.path.basename(sys.argv[0]))

    # Prepare Data
    train_dataset = TVMDataSetV2(args.data,
                                 train_ratio=args.train_ratio if args.eval == 0 else 0.001,
                                 w2v_model=args.w2v_model,
                                 config_path=args.config_path,
                                 shape_info=(args.shape_info == 1),
                                 value_trace=True,
                                 weight_info=(args.weight_info == 1),
                                 is_onehot=True,
                                 is_attr_onehot=True)
    test_dataset = TVMDataSetV2(args.data,
                                train_ratio=args.train_ratio,
                                mode='test',
                                w2v_model=args.w2v_model,
                                config_path=args.config_path,
                                shape_info=(args.shape_info == 1),
                                value_trace=True,
                                weight_info=(args.weight_info == 1),
                                is_onehot=True,
                                is_attr_onehot=True)

    train_dataloader = DataLoader(train_dataset, batch_size=1, shuffle=True, pin_memory=False, num_workers=1)
    test_dataloader = DataLoader(test_dataset, batch_size=1, shuffle=True, pin_memory=False, num_workers=1)

    # Model Init
    net = CLSNetV3Attr(in_d=200,
                       out_classes=SLOT_LENGTH * SLOT_COUNT,
                       hidden=args.hsize,
                       num_layers=args.num_layers,
                       has_shape=(args.shape_info == 1),
                       has_value=True,
                       has_weight_info=(args.weight_info == 1))

    if args.attention == 1:
        net = CLSNet_Attention(in_d=200,
                               out_classes=SLOT_LENGTH * SLOT_COUNT,
                               hiden=args.hsize,
                               num_layers=args.num_layers)
    if not args.ckpt == "None":
        net.load_state_dict(torch.load(args.ckpt))
    net.to(device)

    # Init Optimizor and Loss
    optimizer = torch.optim.Adam(net.parameters(), lr=args.lr)
    if args.optim == "sgd":
        optimizer = torch.optim.SGD(net.parameters(), lr=args.lr)

    # criterion = torch.nn.MSELoss()

    criterion_ce = torch.nn.CrossEntropyLoss()

    # failure_cases = defaultdict(lambda: defaultdict(int))
    if args.eval == 1:
        # net.eval()
        logger.info("Start evaluation ...")
        eval_attr_v2(net, device, test_dataloader, logfile_path)
        exit(0)

    # Training
    for epoch in range(args.epochs):
        pbar = tqdm.tqdm(total=len(train_dataset), file=open(logfile_path, "a+"))
        pbar.set_description("Epoch:{}, Loss:{}".format(epoch + 1, 0))
        count = 0
        sum_loss = 0
        net.train()
        for input, target, shape_info, value_data in train_dataloader:
            pbar.update(1)
            if len(input) == 0:
                continue

            true_functype = label_list[int(target[0][0])]
            type_vector = torch.nn.functional.one_hot(target[0][0].long(), num_classes=len(label_list))
            pred = net(input, device, type_vector, shape_info=shape_info, value_data=value_data)
            pred = pred[0]
            target = target[0]
            optimizer.zero_grad()
            # loss = criterion(pred[0], target[0][1:].to(device))
            attr_list = []
            loss = None
            if true_functype == "conv2d":
                attr_list = [('padding', 0), ('filters', 0), ('strides', 0), ('strides', 1), ('kernel_size', 0),
                             ('depth_multiplier', 0)]
            elif true_functype == "conv3d":
                attr_list = [('padding', 0), ('filters', 0), ('strides', 0), ('strides', 1), ('strides', 2),
                             ('kernel_size', 0)]
            elif true_functype == "upsampling":
                attr_list = [('size', 0), ('interpolation', 0)]
            elif true_functype == "upsampling3d":
                attr_list = [('size', 0)]
            elif true_functype == "pad":
                attr_list = [('padding', 0)]
            elif true_functype == "max_pool2d" or true_functype == "max_pool3d" or true_functype == "avg_pool2d" or true_functype == "avg_pool3d":
                attr_list = [('pool_size', 0), ('padding', 0)]
            elif true_functype == "strided_slice":
                attr_list = [('cropping', 0), ('cropping', 1), ('cropping', 2), ('cropping', 3)]

            # logger.debug(pred)
            # logger.debug(target)
            for i, (attr, pos) in enumerate(attr_list):
                pred_attr_vector = pred[i * SLOT_LENGTH + 1:(i + 1) * SLOT_LENGTH + 1]
                target_attr_vector = target[i + 1]
                target_attr_vector = target_attr_vector.long().to(device)
                if loss is None:
                    loss = criterion_ce(pred_attr_vector.unsqueeze(0), target_attr_vector.unsqueeze(0))
                else:
                    loss += criterion_ce(pred_attr_vector.unsqueeze(0), target_attr_vector.unsqueeze(0))

            if loss is None or torch.isnan(loss):
                continue
            loss.backward()
            optimizer.step()

            sum_loss += loss.item()
            count += 1

            if count % 10 == 0:
                pbar.set_description("Epoch:{}, Loss:{}".format(epoch + 1, sum_loss / count))
        pbar.close()
        logger.info("Epoch:{}, Loss:{}".format(epoch + 1, sum_loss / count))

        # Test
        if epoch % args.eval_per_epoch == 0 or epoch == (args.epochs - 1):
            # Saving the checkpoint
            save_path = "results/{}-({})/checkpoints/".format(current_time, os.path.basename(sys.argv[0]))
            torch.save(net.state_dict(), "{}/model_ckpt_{}.pth".format(save_path, epoch))
            logger.info("Start evaluation ...")
            eval_attr_v2(net, device, test_dataloader, logfile_path)

    logger.info("Finished!")


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='TVM Reversion')
    parser.add_argument("--epochs", default=200, type=int, help="number of epochs")
    parser.add_argument("--lr", default=1e-4, type=float, help="Learning rate")
    parser.add_argument("--hsize", default=200, type=int, help="hidden size of lstm")
    parser.add_argument("--num_layers", default=2, type=int, help="layers of lstm")
    parser.add_argument("--eval_per_epoch", default=2, type=int, help="Do evaluation at every n epochs")
    parser.add_argument("--ckpt", default="None", type=str, help="Checkpoint file")
    parser.add_argument("--optim", default="adam", type=str, help="Optimizer: [adam, sgd]")
    parser.add_argument("--gpu", default="0", type=str)
    parser.add_argument("--eval", default=0, type=int, help="Skip the training step")
    parser.add_argument("--data", default="tvm_data/save_dir/trace_data_all.pkl", type=str, help="The training data")
    parser.add_argument("--w2v_model", default="w2v-new.model", type=str, help="word2vec model")
    parser.add_argument("--attention", default=0, type=int)
    parser.add_argument("--shape_info", default=0, type=int)
    parser.add_argument("--num_class", default=9, type=int)
    parser.add_argument("--weight_info", default=0, type=int)
    parser.add_argument("--train_ratio", default=0.7, type=float)
    parser.add_argument("--config_path", default="tvm_data/other_funcs/elf/", type=str)
    parser.add_argument("--log_level", default="INFO", type=str)

    args = parser.parse_args()
    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu
    run(args)
